<?php
/**
 *---------------------------------------------------------------
 * 檔案名稱: login.php
 * 類別功能: 登入頁。 
 *---------------------------------------------------------------
 */

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller {
	
	//建構方法
	function __construct() {
		parent::__construct();
		$this->load->helper(array('form','url'));
		$this->load->library(array('form_validation','session'));		
	}
	
	//預設方法-處理會員登入
	function index(){				
		
		$this->form_validation->set_rules('login','帳號','trim|required|min_length[6]|max_length[12]|xss_clean');
		$this->form_validation->set_rules('pass','密碼','trim|required|min_length[6]|max_length[12]');
        
		//表單規則驗證
		if($this->form_validation->run() == FALSE){ //驗證失敗
			$this->load->view('frontend/login');
		}else{ //驗證成功			
			
			//載入資料庫 ,並返回結果
			$this->load->database();			
			
			$sql = "SELECT login,name FROM `ci_member` WHERE `login`=? AND `pass`=? ";
			$qry = $this->db->query($sql,array($_POST['login'],$_POST['pass']));
						
			if($qry->num_rows() > 0){ //如果返回會員資料			

				$row = $qry->row();				
				$data = array(
				            'login' => $row->login,
				            'name' => $row->name
				          );
				        
				$this->session->set_userdata($data);				
			    redirect('frontend/home'); //導向首頁
			}else{
				echo "<script>alert('抱歉！無此會員資料!');</script>";
				$this->load->view('frontend/login');
			}				
		}			
	}	
	
	//會員登出的方法
	function logout(){			
		$this->session->sess_destroy();
		//導向首頁,並顯示訊息
		echo "<script>javascript:alert('您已成功登出！'); window.location = '".site_url('frontend/home')."'</script>";
	}
	
}


//*** 結束login.php ***// 